package cn.xk._05tx;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 创建生产者,并指定组名
        TransactionMQProducer producer = new TransactionMQProducer("hello-producer-group");
        // 连接MQ,设置NameServer的地址
        producer.setNamesrvAddr("localhost:9876");
        //设置事务监听
        producer.setTransactionListener(new MyTransactionCheckListener());
        // 启动生产者Producer
        producer.start();

        // 创建消息1，并指定Topic，Tag和消息体
        Message msg1 = new Message("hello-topic5",
                "hello-tag5",
                "收购订单1".getBytes(RemotingHelper.DEFAULT_CHARSET));
        // 发送消息到一个Broker - 发送事务消息
        SendResult res1 = producer.sendMessageInTransaction(msg1, "小红帽");
        System.out.println(res1);

        // 如果不再发送消息，关闭Producer实例。
        producer.shutdown();
    }
}
